package com.whut.monitor.controller;

import com.whut.monitor.rmi.bo.UserAccessBo;
import com.whut.monitor.rmi.service.RmiUserService;
import com.whut.monitor.vo.ResponseMsg;
import com.whut.monitor.vo.SessionInfo;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import java.util.Arrays;
import java.util.LinkedList;
import java.util.List;


/**
 * @author qing
 * 2017/3/22
 * 用户管理接口（远程调用专门的权限管理项目）
 */
@Controller
@RequestMapping("/user")
public class UserController {

    @Resource(name = "userServiceClient")
    private RmiUserService rmiUserService;

    @RequestMapping("/session")
    public String index(HttpServletRequest request) {
        SessionInfo sessionInfo = (SessionInfo) request.getSession().getAttribute("sessionInfo");
        if ((sessionInfo != null) && (sessionInfo.getId() != null)) {
            return "/mintor/main";
        }
        return "/mintor/login";
    }

    @RequestMapping("/login")
    @ResponseBody
    public ResponseMsg login(String loginName, String password, HttpSession session) {
        ResponseMsg responseMsg = new ResponseMsg();
        // TODO 屏蔽远程调用-减少各种错误-以后做安全这块需要使用远程调用
        if ("admin".equals(loginName) && "123".equals(password)) {
            responseMsg.setSuccess(true);
            responseMsg.setMsg("登陆成功！");
            SessionInfo sessionInfo = new SessionInfo();
            sessionInfo.setId(1L);
            sessionInfo.setLoginname("admin");
            sessionInfo.setName("超级管理员");
            sessionInfo.setTenant_id(1L);
            sessionInfo.setTenant_name("cloud");
            List<String> resourceList = new LinkedList<>();
            resourceList.add("");
            resourceList.add("/resource/manager");
            resourceList.add("/role/manager");
            resourceList.add("/tenant/manager");
            resourceList.add("/organization/manager");
            resourceList.add("/resource/treeGrid");
            resourceList.add("/resource/add");
            resourceList.add("/resource/edit");
            resourceList.add("/resource/delete");
            resourceList.add("/role/dataGrid1");
            resourceList.add("/role/add");
            resourceList.add("/role/edit");
            resourceList.add("/role/delete");
            resourceList.add("/role/grant");
            resourceList.add("/tenant/dataGrid1");
            resourceList.add("/tenant/add");
            resourceList.add("/tenant/edit");
            resourceList.add("/tenant/delete");
            resourceList.add("/tenant/view");
            resourceList.add("/organization/treeGrid");
            resourceList.add("/organization/add");
            resourceList.add("/organization/edit");
            resourceList.add("/organization/delete");
            resourceList.add("/organization/view");
            resourceList.add("/user/manager1");
            resourceList.add("/user/dataGrid1");
            resourceList.add("/views/file/upload.jsp");
            resourceList.add("/views/file/download.jsp");
            resourceList.add("/file/add");
            resourceList.add("/file/view");
            resourceList.add("/file/dataGrid");
            resourceList.add("/file/delete");
            resourceList.add("/file/edit");
            resourceList.add("/service/manager");
            resourceList.add("/service/view");
            resourceList.add("/service/dataGrid");
            resourceList.add("/service/add");
            resourceList.add("/service/edit");
            resourceList.add("/service/delete");
            resourceList.add("/service/order");
            resourceList.add("/user/view");
            resourceList.add("/user/add");
            resourceList.add("/user/edit");
            resourceList.add("/user/delete");
            resourceList.add("/tenant/grant");
            resourceList.add("/order/manager");
            resourceList.add("/order/dataGrid");
            resourceList.add("/order/view");
            resourceList.add("/order/use");
            resourceList.add("/order/delete");
            resourceList.add("/order/design");
            resourceList.add("/file/downjudge");
            resourceList.add("baseinfo");
            resourceList.add("/equipment/manager");
            resourceList.add("/channel/manager");
            resourceList.add("/sensor/manager");
            resourceList.add("/equipment/add");
            resourceList.add("/equipment/edit");
            resourceList.add("/equipment/delete");
            resourceList.add("/channel/edit");
            resourceList.add("/channel/add");
            resourceList.add("/channel/delete");
            resourceList.add("/sensor/edit");
            resourceList.add("/sensor/add");
            resourceList.add("/sensor/delete");
            resourceList.add("/machining/add");
            resourceList.add("/machining/edit");
            resourceList.add("/machining/delete");
            resourceList.add("/machining/manager");
            resourceList.add("/timeMachining/add");
            resourceList.add("/timeMachining/edit");
            resourceList.add("/timeMachining/delete");
            resourceList.add("/timeMachining/manager");
            resourceList.add("/pattern/add");
            resourceList.add("/pattern/edit");
            resourceList.add("/pattern/delete");
            resourceList.add("/pattern/manager");
            resourceList.add("/compensator/manager");
            resourceList.add("/compensator/add");
            resourceList.add("/compensator/delete");
            resourceList.add("/compensator/edit");
            resourceList.add("/compensatorEquipment/manager");
            resourceList.add("/compensatorEquipment/add");
            resourceList.add("/compensatorEquipment/edit");
            resourceList.add("/compensatorEquipment/delete");
            resourceList.add("sensorcof");
            resourceList.add("/monitor/manager");
            resourceList.add("/monitor/edit");
            resourceList.add("/monitor/add");
            resourceList.add("/monitor/delete");
//            [, /resource/manager, /role/manager, /tenant/manager, /organization/manager, /resource/treeGrid, /resource/add, /resource/edit, /resource/delete, /role/dataGrid1, /role/add, /role/edit, /role/delete, /role/grant, /tenant/dataGrid1, /tenant/add, /tenant/edit, /tenant/delete, /tenant/view, /organization/treeGrid, /organization/add, /organization/edit, /organization/delete, /organization/view, /user/manager1, /user/dataGrid1, , /views/file/upload.jsp, /views/file/download.jsp, /file/add, /file/view, /file/dataGrid, /file/delete, /file/edit, , /service/manager, /service/view, /service/dataGrid, /service/add, /service/edit, /service/delete, /service/order, /user/view, /user/add, /user/edit, /user/delete, /tenant/grant, /order/manager, /order/dataGrid, /order/view, /order/use, /order/delete, /order/design, /file/downjudge, , baseinfo, /equipment/manager, /channel/manager, /sensor/manager, /equipment/add, /equipment/edit, /equipment/delete, /channel/edit, /channel/add, /channel/delete, /sensor/edit, /sensor/add, /sensor/delete, /compensator/manager, /compensator/add, /compensator/delete, /compensator/edit, /compensatorEquipment/manager, /compensatorEquipment/add, /compensatorEquipment/edit, /compensatorEquipment/delete, sensorcof, /monitor/manager, /monitor/edit, /monitor/add, /monitor/delete]
            sessionInfo.setResourceList(resourceList);
            session.setAttribute("sessionInfo", sessionInfo);
            System.out.println("屏蔽远程调用, admin登录成功！");
        } else {
            responseMsg.setSuccess(false);
            responseMsg.setMsg("用户名或密码错误！");
        }
        return responseMsg;

        // 远程调用-传字符串集合
//        String userAccessBo = rmiUserService.findStrUser(loginName, password);
//        System.out.println("远程调用用户权限结果：outUser:" + userAccessBo);
//        String[] userArray = userAccessBo.split("#");
//        if (userArray.length < 3){
//            responseMsg.setSuccess(false);
//            responseMsg.setMsg("用户名或密码错误！");
//        } else{
//            SessionInfo sessionInfo = new SessionInfo();
//            sessionInfo.setId(Long.valueOf(userArray[0]));
//            sessionInfo.setLoginname(userArray[1]);
//            sessionInfo.setName(userArray[2]);
//            sessionInfo.setTenant_id(Long.valueOf(userArray[3]));
//            sessionInfo.setTenant_name(userArray[4]);
//            userArray[5] = userArray[5].replace("[","");
//            userArray[5] = userArray[5].replace("]","");
//            String[] resourceArray = userArray[5].split(",",-1);
//            List<String> resourceList = Arrays.asList(resourceArray);
//            sessionInfo.setResourceList(resourceList);
//            session.setAttribute("sessionInfo", sessionInfo);
//            responseMsg.setSuccess(true);
//            responseMsg.setMsg("登陆成功！");
//        }
//        return responseMsg;

        // 远程调用-传类（有Bug）
//        UserAccessBo userAccessBo = rmiUserService.findUser(loginName, password);
//        if (userAccessBo != null && userAccessBo.getName() != null) {
//            responseMsg.setSuccess(true);
//            responseMsg.setMsg("登陆成功！");
//            SessionInfo sessionInfo = new SessionInfo();
//            sessionInfo.setId(userAccessBo.getUserId());
//            sessionInfo.setLoginname(userAccessBo.getLoginName());
//            sessionInfo.setName(userAccessBo.getName());
//            sessionInfo.setTenant_id(userAccessBo.getTenantId());
//            sessionInfo.setTenant_name(userAccessBo.getTenantName());
//            sessionInfo.setResourceList(userAccessBo.getResourceList());
//            session.setAttribute("sessionInfo", sessionInfo);
//            System.out.println(userAccessBo.getUserId());
//            System.out.println(userAccessBo.getLoginName());
//            System.out.println(userAccessBo.getName());
//            System.out.println(userAccessBo.getTenantId());
//            System.out.println(userAccessBo.getTenantName());
//            System.out.println(userAccessBo.getResourceList());
//            System.out.println("显示用户名：" + sessionInfo.getName());
//        } else {
//            responseMsg.setSuccess(false);
//            responseMsg.setMsg("用户名或密码错误！");
//        }
//        return responseMsg;

        // 远程调用test代码
//        String name = rmiUserService.sayHello(loginName + password);
//        System.out.println("远程调用测试：" + name);
    }

    @RequestMapping("/quit")
    public String quit(HttpServletRequest request) {
        request.getSession().invalidate();
        return  "redirect:/user/session";
    }

    @ResponseBody
    @RequestMapping("/logout")
    public ResponseMsg logout(HttpSession session) {
        ResponseMsg responseMsg = new ResponseMsg();
        if (session != null) {
            session.invalidate();
        }
        responseMsg.setSuccess(true);
        responseMsg.setMsg("注销成功！");
        return responseMsg;
    }

}
